<?php
namespace app\command;

use app\beautiful\model\partner\BonusModel;
use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\setting\PartnerBonusSettingModel;
use app\beautiful\model\setting\PartnerTaskBonusSettingModel;
use app\common\model\MemberMoneyDetailModel;
use app\common\model\ShopBalanceLogsModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Partnerbonus_end extends Command
{
    protected function configure()
    {
        $this->setName('Partnerbonus_end')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $h=date('H');
        $i=date('i');
        $name = trim($input->getArgument('name'));
        if(($h!='00' || $i!='20') && empty($name)){
            $output->writeln("暂不能执行！每天凌晨20分执行" );exit();
        }
        do{
            $s=[];
            $s['status']=2;
            $s['bonus_id']=0;
            $list=PartnerBonusModel::where($s)->field('COUNT(partner_bonus_id) as num,sum(bonus_price) as all_bonus_price,start_time,end_time,shop_id,type,edit_time')->group('shop_id,type')->select()->toArray();
            if(empty($list)){
                $output->writeln("暂无数据".var_export([$s],true) );exit();
            }
            foreach ($list as $v){
                $where=[];
                $where['shop_id']=$v['shop_id'];
                $where['start_time']=strtotime($v['start_time']);
                $where['end_time']=strtotime($v['end_time']);
                $reward_money=partner_reward_money($where);
                $p_s=[];
                $p_s[]=['shop_id','=',$v['shop_id']];
                $p_s[]=['status','=',2];
                $p_s[]=['bonus_id','=',0];
                $p_s[]=['type','=',$v['type']];
                $p_s[]=['start_time','=',$v['start_time']];
                $p_s[]=['bonus_price','>',0];
                $bonus_num=PartnerBonusModel::where($p_s)->count();
                $task_detail=PartnerTaskBonusSettingModel::where(['shop_id'=>$where['shop_id']])->find()->toArray();
                $bonus_setting=PartnerBonusSettingModel::where(['shop_id'=>$where['shop_id']])->find()->toArray();
                $bonus_in=[];
                $bonus_in['shop_id']=$v['shop_id'];
                $bonus_in['bonus_cycle_type']=$v['type'];
                $bonus_in['start_time']=$v['start_time'];
                $bonus_in['end_time']=$v['end_time'];
                $bonus_in['receive_time']=$v['edit_time'];
                $bonus_in['bonus_pool']=$reward_money;
                $bonus_in['bonus_total_price']=$v['all_bonus_price'];
                $bonus_in['partner_num']=$v['num'];
                $bonus_in['partner_bonus_num']=$bonus_num;
                $bonus_in['per_share_price']=empty($bonus_setting)?0:$bonus_setting['per_share_price'];
                $bonus_in['total_share_num']=empty($bonus_setting)?0:$bonus_setting['total_share_num'];
                $bonus_in['join_bonus_role_id']=empty($bonus_setting)?'':$bonus_setting['join_bonus_role_id'];
                $bonus_in['bonus_budget']=empty($bonus_setting)?0:$bonus_setting['bonus_budget'];
                $bonus_in['bonus_budget_type']=empty($bonus_setting)?0:$bonus_setting['bonus_budget_type'];
                $bonus_in['task_status']=empty($task_detail)?0:$task_detail['status'];
                $bonus_in['join_task_bonus_role_id']=empty($task_detail)?0:$task_detail['join_role_id'];
                $consume=[];
                $toker=[];
                $promotion=[];
                if(!empty($task_detail)){
                    $consume['consume_task_type']=$task_detail['consume_task_type'];
                    $consume['consume_price']=$task_detail['consume_price'];
                    $consume['consume_type']=$task_detail['consume_type'];
                    $consume['consume_bonus_ratio']=$task_detail['consume_bonus_ratio'];
                    $toker['toker_task_type']=$task_detail['toker_task_type'];
                    $toker['toker_consume_price']=$task_detail['toker_consume_price'];
                    $toker['toker_consume_type']=$task_detail['toker_consume_type'];
                    $toker['toker_bonus_ratio']=$task_detail['toker_bonus_ratio'];
                    $promotion['promotion_task_type']=$task_detail['promotion_task_type'];
                    $promotion['promotion_share_num']=$task_detail['promotion_share_num'];
                    $promotion['promotion_bonus_ratio']=$task_detail['promotion_bonus_ratio'];
                    $promotion['is_memeber_once']=$task_detail['is_memeber_once'];
                    $promotion['same_member_time']=$task_detail['same_member_time'];
                }
                $bonus_in['cousume_detail']=json_encode($consume);
                $bonus_in['toker_detail']=json_encode($toker);
                $bonus_in['promotion_detail']=json_encode($promotion);
                $bonus_in['is_end']=1;
                $bonus_in['add_time']=date('Y-m-d H:i:s');
                $bonus_id=BonusModel::insertGetId($bonus_in);
                $u_s=[];
                $u_s[]=['shop_id','=',$v['shop_id']];
                $u_s[]=['status','=',2];
                $u_s[]=['bonus_id','=',0];
                $u_s[]=['type','=',$v['type']];
                $u_s[]=['start_time','=',$v['start_time']];
                PartnerBonusModel::up(['bonus_id'=>$bonus_id],$u_s);
                //股东分红奖金退回商家账户
                $money=$reward_money-$v['all_bonus_price'];
                if(!empty($money)){
                    ShopBalanceLogsModel::in($money, 1, $v['shop_id'], $bonus_id, 4,1);
                }
            }
        }while(true);
    }
}